package com.collection;

import java.util.ArrayList;
import java.util.Iterator;

/**
 * @author cjx
 * @create 2024/06/28
 * @description 集合 进阶
 */
public class CollectionTest {
    public static void main(String[] args) {
        // 迭代器、增强for、forEach 不需要依赖索引遍历集合
//        itratorTest();
//        strongFor();
//        lanmuda();

        containTest();
    }

    // 迭代器
    private static void itratorTest() {
        ArrayList<Integer> arr = new ArrayList<>();

        arr.add(1);
        arr.add(2);
        arr.add(3);
        arr.add(4);
        arr.add(5);

        Iterator it = arr.iterator();
        while (it.hasNext()) {
            int item = (int) it.next();
            System.out.println(it.next());
        }
    }

    // 增强for
    private static void strongFor() {
        ArrayList<Integer> arr = new ArrayList<>();

        arr.add(1);
        arr.add(2);
        arr.add(3);
        arr.add(4);
        arr.add(5);

        for (Integer item : arr) {
            System.out.println(item);
        }
    }

    // 箭头函数
    private static void lanmuda() {
        ArrayList<Integer> arr = new ArrayList<>();

        arr.add(1);
        arr.add(2);
        arr.add(3);
        arr.add(4);
        arr.add(5);

//        arr.forEach(new Consumer<Integer>() {
//            @Override
//            public void accept(Integer integer) {
//                System.out.println(integer);
//            }
//        });
        arr.forEach(item -> System.out.println(item));
    }

    private static void containTest() {
        ArrayList<Student> arr = new ArrayList<>();
        Student s1 = new Student("小明", 19);
        Student s2 = new Student("小三", 18);
        Student s3 = new Student("小明", 19);
        Student s4 = new Student("小明", 11);
        arr.add(s1);
        arr.add(s2);
        arr.add(s3);

        System.out.println(arr.contains(s4));
        System.out.println(arr.equals(s1));
    }
    public static class Student {
        private String name;
        private int age;

        public Student(String name, int age) {
            this.name = name;
            this.age = age;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }
    }
}
